package com.woniuxy.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.finance.mapper.RechargeMapper;
import com.woniuxy.finance.model.dto.ConditionsFindBusDto;
import com.woniuxy.finance.model.dto.UpdateBus;
import com.woniuxy.finance.model.entity.BusinessCompetitor;
import com.woniuxy.finance.mapper.BusinessCompetitorMapper;
import com.woniuxy.finance.model.entity.Recharge;
import com.woniuxy.finance.model.vo.ClientVo;
import com.woniuxy.finance.model.vo.FindClientNameAndBalanceVo;
import com.woniuxy.finance.service.BusinessCompetitorService;
import com.woniuxy.util.SnowflakeIdWorker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 友商信息表; 服务实现类
 * </p>
 *
 * @author chenlin
 * @since 2023-09-06
 */
@Service
public class BusinessCompetitorServiceImpl extends ServiceImpl<BusinessCompetitorMapper, BusinessCompetitor> implements BusinessCompetitorService {
    @Resource
    private BusinessCompetitorMapper businessCompetitorMapper;
    @Resource
    private RechargeMapper rechargeMapper;
    /**
     * 查询所有客户信息
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<BusinessCompetitor> findClientAll(Integer type,Integer page, Integer size) {
        BusinessCompetitor businessCompetitor = new BusinessCompetitor();
        businessCompetitor.setType(type);
        businessCompetitor.setStatus("0");
        QueryWrapper<BusinessCompetitor> queryWrapper = new QueryWrapper<>(businessCompetitor);
        Page<BusinessCompetitor> pages = businessCompetitorMapper.selectPage(new Page<>(page, size),queryWrapper);
        return pages;
    }
    /**
     * 按启用/禁用查询客户信息
     * @param acct
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<BusinessCompetitor> findClientByAcct(String acct,Integer type, Integer page, Integer size) {
        BusinessCompetitor businessCompetitor = new BusinessCompetitor();
        businessCompetitor.setType(type);
        businessCompetitor.setAcct(acct);
        QueryWrapper<BusinessCompetitor> queryWrapper = new QueryWrapper<>(businessCompetitor);
        Page<BusinessCompetitor> pages = businessCompetitorMapper.selectPage(new Page<>(page, size),queryWrapper);
        return pages;
    }
    /**
     * 新增友商
     * @param businessCompetitor
     * @param account
     * @return
     */
    @Transactional
    @Override
    public boolean addClient(BusinessCompetitor businessCompetitor, String account) {
        businessCompetitor.setUniformCreditCode(SnowflakeIdWorker.newId()+"");
        businessCompetitor.setBalance(new BigDecimal(0));
        businessCompetitor.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        businessCompetitor.setVipLevel(0);
        businessCompetitor.setAdminName(account);
        businessCompetitor.setAtt("1");
        businessCompetitor.setAcct("1");
        businessCompetitor.setStatus("0");
        if(businessCompetitorMapper.insert(businessCompetitor) > 0){
            return true;
        }
        return false;
    }
    /**
     * 多条件查询友商
     * @param condition,type
     * @return
     */
    @Override
    public Page<BusinessCompetitor> conditionsFindBus(ConditionsFindBusDto condition) {
            BusinessCompetitor b1 = new BusinessCompetitor();
            b1.setType(condition.getType());
            b1.setName(condition.getCondition());
            QueryWrapper<BusinessCompetitor> q1 = new QueryWrapper<>(b1);
            Page<BusinessCompetitor> p1 = businessCompetitorMapper.selectPage(new Page<>(condition.getPage(), condition.getSize()), q1);
            if (p1.getRecords().size() == 0 || p1.getRecords() == null) {
                BusinessCompetitor b2 = new BusinessCompetitor();
                b2.setType(condition.getType());
                b2.setUniformCreditCode(condition.getCondition());
                QueryWrapper<BusinessCompetitor> q2 = new QueryWrapper<>(b2);
                return businessCompetitorMapper.selectPage(new Page<>(condition.getPage(), condition.getSize()), q2);
            }
            return p1;
    }
    /**
     * 修改友商信息
     */
    @Transactional
    @Override
    public boolean updateBusById(UpdateBus updateBus) {
        BusinessCompetitor businessCompetitor = new BusinessCompetitor();
        businessCompetitor.setId(updateBus.getId());
        businessCompetitor.setName(updateBus.getName());
        businessCompetitor.setAbbreviation(updateBus.getAbbreviation());
        businessCompetitor.setMark(updateBus.getMark());
        businessCompetitor.setEncoding(updateBus.getEncoding());
        businessCompetitor.setArea(updateBus.getArea());
        businessCompetitor.setEmail(updateBus.getEmail());
        businessCompetitor.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        int i = businessCompetitorMapper.updateById(businessCompetitor);
        if(i > 0 ){
            return true;
        }
        return false;
    }
    /**
     * 关注友商
     * @param id
     * @return
     */
    @Transactional
    @Override
    public boolean attBusById(Integer id) {

        BusinessCompetitor businessCompetitor = new BusinessCompetitor();
        businessCompetitor.setId(id);
        businessCompetitor.setAtt("0");
        businessCompetitor.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        int i = businessCompetitorMapper.updateById(businessCompetitor);
        if(i > 0 ){
            return true;
        }
        return false;
    }
    /**
     * 取消关注
     * @param id
     * @return
     */
    @Transactional
    @Override
    public boolean noAttBusById(Integer id) {
        BusinessCompetitor businessCompetitor = new BusinessCompetitor();
        businessCompetitor.setId(id);
        businessCompetitor.setAtt("1");
        int i = businessCompetitorMapper.updateById(businessCompetitor);
        if(i > 0 ){
            return true;
        }
        return false;
    }
    /**
     * 启用
     * @param id
     * @return
     */
    @Transactional
    @Override
    public boolean openById(Integer id) {
        //充值单状态改为0
        //根据id获取账户名
        BusinessCompetitor b = businessCompetitorMapper.selectById(id);
        Recharge recharge = new Recharge();
        recharge.setUserName(b.getName());
        //查询该友商是否有充值单
        List<Recharge> list = rechargeMapper.selectList(new QueryWrapper<>(recharge));
        if(list != null){
            recharge.setStatus("0");
            rechargeMapper.updateStatusByName(recharge);
        }
        BusinessCompetitor businessCompetitor = new BusinessCompetitor();
        businessCompetitor.setId(id);
        businessCompetitor.setAcct("0");
        int i = businessCompetitorMapper.updateById(businessCompetitor);
        if(i > 0 ){
            return true;
        }
        return false;
    }
    /**
     * 禁用
     * @param id
     * @return
     */
    @Transactional
    @Override
    public boolean closeById(Integer id) {
        //充值单状态改为1
        //根据id获取账户名
        BusinessCompetitor b = businessCompetitorMapper.selectById(id);
        Recharge recharge = new Recharge();
        recharge.setUserName(b.getName());
        //查询该友商是否有充值单
        List<Recharge> list = rechargeMapper.selectList(new QueryWrapper<>(recharge));
        if(list != null){
            recharge.setStatus("1");
            rechargeMapper.updateStatusByName(recharge);
        }
        BusinessCompetitor businessCompetitor = new BusinessCompetitor();
        businessCompetitor.setId(id);
        businessCompetitor.setAcct("1");
        int i = businessCompetitorMapper.updateById(businessCompetitor);
        if(i > 0 ){
            return true;
        }
        return false;
    }
    /**
     * 查询所有供应商信息
     * @param page
     * @param size
     * @return
     */
//    @Override
//    public Page<BusinessCompetitor> findVendorAll(Integer page, Integer size) {
//        BusinessCompetitor businessCompetitor = new BusinessCompetitor();
//        businessCompetitor.setType(1);
//        QueryWrapper<BusinessCompetitor> queryWrapper = new QueryWrapper<>(businessCompetitor);
//        Page<BusinessCompetitor> pages = businessCompetitorMapper.selectPage(new Page<>(page, size),queryWrapper);
//        return pages;
//    }
    /**
     * 按状态查询供应商信息
     * @param status
     * @param page
     * @param size
     * @return
     */
//    @Override
//    public Page<BusinessCompetitor> findVendorByStatus(String status, Integer page, Integer size) {
//        BusinessCompetitor businessCompetitor = new BusinessCompetitor();
//        businessCompetitor.setType(1);
//        businessCompetitor.setAcct(status);
//        QueryWrapper<BusinessCompetitor> queryWrapper = new QueryWrapper<>(businessCompetitor);
//        Page<BusinessCompetitor> pages = businessCompetitorMapper.selectPage(new Page<>(page, size),queryWrapper);
//        return pages;
//    }
    /**
     * 新增供应商
     * @param businessCompetitor
     * @param account
     * @return
     */
//    @Transactional
//    @Override
//    public boolean addVendor(BusinessCompetitor businessCompetitor, String account) {
//        businessCompetitor.setType(1);
//        businessCompetitor.setUniformCreditCode(SnowflakeIdWorker.newId()+"");
//        businessCompetitor.setBalance(new BigDecimal(0));
//        businessCompetitor.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
//        businessCompetitor.setVipLevel(0);
//        businessCompetitor.setAdminName(account);
//        businessCompetitor.setAtt("1");
//        businessCompetitor.setStatus("1");
//        if(businessCompetitorMapper.insert(businessCompetitor) > 0){
//            return true;
//        }
//        return false;
//    }
    /**
     * 查询关注的友商信息
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<BusinessCompetitor> findBus(Integer page,Integer size) {
        BusinessCompetitor businessCompetitor = new BusinessCompetitor();
        businessCompetitor.setAtt("0");
        businessCompetitor.setAcct("0");
        QueryWrapper queryWrapper = new QueryWrapper<>(businessCompetitor);
        return businessCompetitorMapper.selectPage(new Page<>(page,size),queryWrapper);
    }
    /**
     * 查询关注的客户、供应商 信息
     * @param type
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<BusinessCompetitor> findBusByType(Integer type, Integer page, Integer size) {
        BusinessCompetitor businessCompetitor = new BusinessCompetitor();
        businessCompetitor.setAtt("0");
        businessCompetitor.setAcct("0");
        businessCompetitor.setType(type);
        QueryWrapper queryWrapper = new QueryWrapper<>(businessCompetitor);
        return businessCompetitorMapper.selectPage(new Page<>(page,size),queryWrapper);
    }
    /**
     * 多条件查询关注的友商信息
     * @param condition
     * @return
     */
    @Override
    public Page<BusinessCompetitor> conditionsFindAtt(String condition, Integer page, Integer size) {
        BusinessCompetitor b1 = new BusinessCompetitor();
        b1.setName(condition);
        b1.setAtt("0");
        b1.setAcct("0");
        QueryWrapper<BusinessCompetitor> q1 = new QueryWrapper<>(b1);
        Page<BusinessCompetitor> p1 = businessCompetitorMapper.selectPage(new Page<>(page, size), q1);
        if (p1.getRecords().size() == 0 || p1.getRecords() == null) {
            BusinessCompetitor b2 = new BusinessCompetitor();
            b2.setUniformCreditCode(condition);
            b2.setAtt("0");
            b2.setAcct("0");
            QueryWrapper<BusinessCompetitor> q2 = new QueryWrapper<>(b2);
            return businessCompetitorMapper.selectPage(new Page<>(page,size), q2);
        }
        return p1;
    }

    /**
     * 获取所有客户名称
     * @return
     */
    @Override
    public List<ClientVo> getName() {
        BusinessCompetitor businessCompetitor = new BusinessCompetitor();
        businessCompetitor.setType(0);
        businessCompetitor.setAcct("0");
        businessCompetitor.setStatus("0");
        return businessCompetitorMapper.getName(businessCompetitor);
    }

    @Override
    public List<BusinessCompetitor> findSupplierBycondition(String name, String status, Integer type) {
        LambdaQueryWrapper<BusinessCompetitor> lqw = new LambdaQueryWrapper<>();
        lqw.like(name!=null,BusinessCompetitor::getName,name)
                .eq(BusinessCompetitor::getStatus,status)
                .eq(BusinessCompetitor::getType,type)
                .eq(BusinessCompetitor::getAcct,"0");
        return businessCompetitorMapper.selectList(lqw);
    }

    /**
     * 删除友商
     * @param id
     * @return
     */
    @Override
    public boolean delBusById(Integer id) {
        //判断客户余额是否为0 不为0 返回false
        BusinessCompetitor b = businessCompetitorMapper.selectById(id);
        if(b.getBalance().compareTo(new BigDecimal(0)) > 0){
            return false;
        }
        //判断客户是否还有未完成的账单 有 返回false

        //软删除   把当前客户的状态改为1
        BusinessCompetitor businessCompetitor = new BusinessCompetitor();
        businessCompetitor.setId(id);
        businessCompetitor.setStatus("1");
        int i = businessCompetitorMapper.updateById(businessCompetitor);
        if(i > 0){
            return true;
        }
        return false;
    }
    /**
     * 获取客户名和余额
     *  按余额降序
     *  limit 6
     */
    @Override
    public List<FindClientNameAndBalanceVo> findClientNameAndBalance() {
        return businessCompetitorMapper.findClientNameAndBalance();
    }
    /**
     *获取客户名和充值总额
     * 按总额降序
     * limit 6
     */
    @Override
    public List<FindClientNameAndBalanceVo> findClientNameAndRechargeTotal() {
        return businessCompetitorMapper.findClientNameAndRechargeTotal();
    }
    /**
     * 根据id获取邮箱
     */
    @Override
    public String getEmailById(Integer id) {
        BusinessCompetitor b = new BusinessCompetitor();
        b.setId(id);
        return businessCompetitorMapper.getEmailById(b);
    }

}
